package com.bjdd.jdy.ui.working;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.DistanceUtil;
import com.bjdd.jdy.Constant;
import com.bjdd.jdy.baidu.LocationUtils;
import com.bjdd.jdy.entity.OrderBean;
import com.bjdd.jdy.entity.User;
import com.bjdd.jdy.util.JumpUtil;
import com.bjdd.jdy.util.L;
import com.bjdd.jdy.util.SpUtil;
import com.bjdd.jdy.util.T;

import rx.Observable;

/**
 * Created by Administrator on 2017/5/2.
 */

public class WorkingPresenter extends WorkingContract.Presenter {
    private Observable grabObservable;
    @Override
    public void onStart() {
        if (SpUtil.getUser() == null || SpUtil.getUser().equals("") || SpUtil.getUser().getCarrierId() == null) {
            JumpUtil.toLogin(context);
            return;
        }
        //获取今日收益和工作时长
        getTodayData();
    }

    @Override
    void getTodayData() {
        String token = SpUtil.getToken();
        String carrierId = SpUtil.getUser().getCarrierId();
        rxManager.add(model.getTodayData(token,carrierId)
        .subscribe(result -> {
            if (!result.isError()){
                view.todatData(result.getData());
            }
        },e -> {

        }));
    }

    @Override
    void startWork() {
        String token = SpUtil.getToken();
        String carrierId = SpUtil.getUser().getCarrierId();
        showLoadingDialog(false);
        rxManager.add(model.start(token,carrierId)
                .subscribe(result -> {
                    hideLoadingDialog();
                    if (result.isError()){
                        T.show(result.getMsg());
                    }else {
                        User user = SpUtil.getUser();
                        user.setWorkStatus(true);
                        SpUtil.setUser(user);
                        view.startWorkBack();
                    }
                },e -> {
                    hideLoadingDialog();
                    T.show("开工失败");
                    L.d("开工失败 : "+e.getMessage());
                }));
    }

    @Override
    void finshWork() {
        String token = SpUtil.getToken();
        String carrierId = SpUtil.getUser().getCarrierId();
        showLoadingDialog(false);
        rxManager.add(model.stop(token,carrierId)
                .subscribe(result -> {
                    hideLoadingDialog();
                    if (result.isError()){
                        T.show(result.getMsg());
                    }else {
                        User user = SpUtil.getUser();
                        user.setWorkStatus(false);
                        SpUtil.setUser(user);
                        view.stopWorkBack();
                    }
                },e -> {
                    hideLoadingDialog();
                    T.show("收工失败");
                }));
    }


    @Override
    void queryWaitOrder(int page, int size) {
        //showLoadingDialog(false);
        if (LocationUtils.location == null){
            //T.show("定位尚未完成，请稍后重试");
            return;
        }
        String cityName = LocationUtils.location.getCity();
        String token = SpUtil.getToken();
        String carrierId = SpUtil.getUser().getCarrierId();
        rxManager.add(model.queryWaitOrder(token,cityName,page,size)
        .subscribe(result -> {
            if (!result.isError()){
                view.orderBack(Constant.ORDER_PAID,result.getData().getContent());
            }else {
                view.loadingError();
                T.show(result.getMsg());
            }
        },e ->{
        }));
    }

    @Override
    void queryOrder(int type, int page, int size) {
        String token = SpUtil.getToken();
        String carrierId = SpUtil.getUser().getCarrierId();
        rxManager.add(model.queryOrder(token,carrierId,type,page,size)
                .subscribe(result -> {
                    if (!result.isError()){
                        view.orderBack(type,result.getData().getContent());
                    }else {
                        T.show(result.getMsg());
                    }
                },e ->{
                    view.loadingError();
                    T.show("加载失败");
                    L.d("订单获取错误："+e.getMessage());
                }));
    }

    @Override
    void onPause() {
        if (grabObservable != null){
            rxManager.unregister(Constant.GRAB, grabObservable);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        grabObservable = rxManager.on1(Constant.GRAB, msg -> view.showGrapDialog((long)msg));
    }

    void grab(long orderId){
        L.d("抢单");
        L.d("wocao","抢单");
        showLoadingDialog(false);
        String token = SpUtil.getToken();
        String carrierId = SpUtil.getUser().getCarrierId();
        rxManager.add(model.grab(token,carrierId,orderId)
        .subscribe(result -> {
            hideLoadingDialog();
            if (!result.isError()){
                view.grabBack();
            }else {
                T.show(result.getMsg());
            }
        },e -> {
            hideLoadingDialog();
            T.show("抢单失败");
        }));
    }

}
